﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Cms.Domain;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Cms.Tests.Unit.Domain
{

    [TestClass]
    public class DetailTemplateTests
    {

        [TestMethod]
        public void GetPublishedContentHtmlAndTitle_AreaAndContentExists_Success()
        {
            DetailTemplate detailTemplate = new DetailTemplate();
            Content c = new Content();
            Locale locale = new Locale { ID = 1 };
            Administrator admin = new Administrator() { ID = 1 };
            DetailTemplateContentLink detailTemplateLink = new DetailTemplateContentLink
            {
                Content = c,
                DetailTemplate = detailTemplate,
                AreaID = "dtcHeader"
            };
            detailTemplate.Content.Add(detailTemplateLink);
            ContentTranslationVersion ctv = c.AddVersion(locale, DateTime.Now.Ticks, "some html", "title", admin);
            ctv.Translation.PublishedVersion = ctv;
            Assert.AreEqual("some html", detailTemplate.GetPublishedContentHtml("dtcHeader", 1));
            Assert.AreEqual("title", detailTemplate.GetPublishedContentTitle("dtcHeader", 1));
        }


        [TestMethod]
        public void GetContentHtmlAndTitle_AreaAndContentExists_Success()
        {
            DetailTemplate detailTemplate = new DetailTemplate();
            Content c = new Content();
            Locale locale = new Locale { ID = 1 };
            Administrator admin = new Administrator() { ID = 1 };
            DetailTemplateContentLink detailTemplateLink = new DetailTemplateContentLink
            {
                Content = c,
                DetailTemplate = detailTemplate,
                AreaID = "dtcHeader"
            };
            detailTemplate.Content.Add(detailTemplateLink);
            c.AddVersion(locale, 1001, "html 1", "title 1", admin);
            c.AddVersion(locale, 1002, "html 2", "title 2", admin);
            c.AddVersion(locale, 1003, "html 3", "title 3", admin);
            Assert.AreEqual("html 2", detailTemplate.GetContentHtml("dtcHeader", 1, "1002"));
            Assert.AreEqual("title 2", detailTemplate.GetContentTitle("dtcHeader", 1, "1002"));
        }


        [TestMethod]
        public void GetContent_AreaAndContentExists_Success()
        {
            DetailTemplate dt = new DetailTemplate();
            Content c = new Content { ID = 1 };
            DetailTemplateContentLink dtLink = new DetailTemplateContentLink { Content = c, DetailTemplate = dt, AreaID = "dtcMain" };
            dt.Content.Add(dtLink);
            Assert.AreEqual(1, dt.GetContent("dtcMain").ID);
        }


        [TestMethod]
        public void AddContentLink_NoPreviousContentInArea_Success()
        {
            DetailTemplate dt = new DetailTemplate();
            Content c = new Content { ID = 1 };
            dt.AddContentLink("dtcMain", c);
            Assert.AreSame(c, dt.GetContent("dtcMain"));
        }


        [TestMethod]
        public void AddContentLink_ExistingContentHasVersions_ExistingVersionsAreDeletedFromContentSet()
        {
            DetailTemplate dt = new DetailTemplate();
            Locale locale = new Locale { ID = 1 };
            Administrator admin = new Administrator();
            Content c = new Content { ID = 1 };
            long timestamp = DateTime.Now.Ticks;
            dt.AddContentLink("dtcMain", c);
            ContentTranslationVersion ctv = c.AddVersion(locale, timestamp, "1", "1", admin);
            dt.GetContentSet(locale).TranslationVersions.Add(ctv);
            ctv = c.AddVersion(locale, timestamp + 600000000, "1", "1", admin); // + 1 minute
            dt.GetContentSet(locale).TranslationVersions.Add(ctv);
            ctv = c.AddVersion(locale, timestamp + 1200000000, "1", "1", admin); // + 2 minutes
            dt.GetContentSet(locale).TranslationVersions.Add(ctv);
            Assert.AreEqual(3, dt.GetContentSet(locale).TranslationVersions.Count);
            // Now select another content
            Content c2 = new Content();
            dt.AddContentLink("dtcMain", c2);
            Assert.AreEqual(0, dt.GetContentSet(locale).TranslationVersions.Count);
        }

    }

}
